﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

namespace UFSSWebApp
{
    public partial class mysubs : System.Web.UI.Page
    {
        UFSSservicereference.UserCreds uc = new UFSSservicereference.UserCreds();
        UFSSservicereference.UFSSserviceSoapClient ussc = new UFSSservicereference.UFSSserviceSoapClient();

        private ArrayList sublist = new ArrayList();
        private ArrayList subtolist = new ArrayList();
        private UFSSservicereference.UFSSSubscription[] subs;
        private UFSSservicereference.UFSSSubscription[] substo;
        private UFSSservicereference.UFSSSubscription sub;
        private UFSSservicereference.UFSSSubscription subto;

        protected void Page_Load(object sender, EventArgs e)
        {
            uc = (UFSSservicereference.UserCreds)Session["usercreds"];
            Session.Remove("latfile");
            subs = ussc.getSubs(uc);
            substo = ussc.getSubbedTo(uc);
            
            if (!IsPostBack)
                refresh();
        }

        private void refresh()
        {
            try
            {
                sublist.Clear();
                subtolist.Clear();
                lstSubs.Items.Clear();
                lstSubbedTo.Items.Clear();

                lstSubs.Enabled = true;
                lstSubbedTo.Enabled = true;

                //subscriptions
                for (int i = 0; i < subs.Length; i++)
                    sublist.Add(subs[i]);

                foreach (UFSSservicereference.UFSSSubscription sub in sublist)
                    lstSubs.Items.Add(sub.subName);

                if (lstSubs.Items.Count == 0)
                {
                    lstSubs.Items.Add("No Subscriptions");
                    lstSubs.Enabled = false;
                    lblSubs.Text = "";
                    lblFiles.Text = "";
                    lblLatfile.Text = "";
                    lblLastUp.Text = "";
                }

                //subbed to
                for (int i = 0; i < substo.Length; i++)
                    subtolist.Add(substo[i]);

                foreach (UFSSservicereference.UFSSSubscription subto in subtolist)
                    lstSubbedTo.Items.Add(subto.subName);

                if (lstSubbedTo.Items.Count == 0)
                {
                    lstSubbedTo.Items.Add("Not Subscribed");
                    lstSubbedTo.Enabled = false;
                    lblSubs2.Text = "";
                    lblFiles2.Text = "";
                    lblLatFile2.Text = "";
                    lblLastUp2.Text = "";
                }
                //hlLatFile.Text = "";
                lblError.Text = "";
                lblError2.Text = "";
            }
            catch
            {
            }
        }

        protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session.Remove("sub");
            Session.Remove("latfile");

            Session.Add("sub", lstSubs.SelectedItem.ToString());

            try
            {
                lblSubs.Text = subs[lstSubs.SelectedIndex].members.ToString();
                lblFiles.Text = subs[lstSubs.SelectedIndex].files.ToString();
                lblLatfile.Text = subs[lstSubs.SelectedIndex].latfile;
                lblLastUp.Text = subs[lstSubs.SelectedIndex].lastup.ToString();
                sub = ussc.getSubInfo(Session["sub"].ToString());

                Session.Add("latfile", sub.latfile.ToString());
                
                
            }
            catch
            {
                lblSubs.Text = subs[lstSubs.SelectedIndex].members.ToString();
                lblFiles.Text = subs[lstSubs.SelectedIndex].files.ToString();
                lblLastUp.Text = "No updates";
                
            }
        }

        protected void btnDelSub_Click(object sender, EventArgs e)
        {
            try
            {
                if (lstSubs.SelectedIndex != -1)
                {
                    try
                    {
                        ussc.deleteSub(uc, lstSubs.SelectedItem.ToString());
                        refresh();
                    }
                    catch
                    {
                        lblError.Text = "Error removing subscription.";
                    }
                }
                else
                    lblError.Text = "Error removing subscription.";
            }
            catch
            {
            }
        }

        protected void btnViewSub_Click(object sender, EventArgs e)
        {
            try
            {
                if (lstSubs.SelectedIndex != -1)
                {
                    Session.Remove("sub");
                    Session.Remove("user");
                    Session.Add("sub", lstSubs.SelectedItem.ToString());
                    Session.Add("user", uc.Username.ToString());
                    Response.Redirect("~/usersubs.aspx");
                }
                else
                    lblError.Text = "Please select a subscription to view.";
            }
            catch
            {
                lblError.Text = "An error occured.";
            }
        }

        protected void lbtnFile_Click(object sender, EventArgs e)
        {
            
            //Session.Add("fileinfo", );
            Response.Redirect("~/userfile.aspx");
        }

        protected void lstSubbedTo_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                Session.Remove("sub");
                Session.Remove("latfile");

                Session.Add("sub", lstSubbedTo.SelectedItem.ToString());

                try
                {
                    lblSubs2.Text = substo[lstSubbedTo.SelectedIndex].members.ToString();
                    lblFiles2.Text = substo[lstSubbedTo.SelectedIndex].files.ToString();
                    lblLatFile2.Text = substo[lstSubbedTo.SelectedIndex].latfile;
                    lblLastUp2.Text = substo[lstSubbedTo.SelectedIndex].lastup.ToString();
                    subto = ussc.getSubInfo(Session["sub"].ToString());

                    Session.Add("latfile", subto.latfile.ToString());

                }
                catch
                {
                    lblSubs2.Text = substo[lstSubbedTo.SelectedIndex].members.ToString();
                    lblFiles2.Text = substo[lstSubbedTo.SelectedIndex].files.ToString();
                    lblLastUp2.Text = "No updates";

                }
            }
            catch
            {
            }
        }

        protected void btnLeaveSub_Click(object sender, EventArgs e)
        {
            try
            {
                if (lstSubbedTo.SelectedIndex != -1)
                {
                    ussc.leaveSubscription(uc, lstSubbedTo.SelectedItem.ToString());
                    refresh();
                }
                else
                    lblError2.Text = "Please select a subscription.";
            }
            catch
            {
                lblError2.Text = "Error leaving subscription.";
            }
        }
    }
}